#include <math.h>
#include <stdlib.h>
#include <time.h>

#define M_PI 3.14159265358979323846

// 生成载体信号：正弦波
int generate_cover_signal(double *cover, const int size) {
    const double frequency = 1000.0; // 载体频率1 kHz
    const double sampling_rate = 44100.0; // 采样率44.1 kHz
    for (int i = 0; i < size; i++) {
        cover[i] = sin(2 * M_PI * frequency * i / sampling_rate);
    }
    return 0;
}

// 生成数字调制信号：随机二进制序列
int simulate_digital_modulation_signal(unsigned char *message, const int size) {
    srand(time(NULL)); // 初始化随机种子
    for (int i = 0; i < size; i++) {
        message[i] = rand() % 2 ? 1 : 0; // 非0表示1，0表示0
    }
    return 0;
}

// 生成模拟调制信号：低频正弦波
int simulate_analog_modulation_signal(double *message, const int size) {
    const double frequency = 100.0; // 调制信号频率100 Hz
    const double sampling_rate = 44100.0; // 采样率44.1 kHz
    for (int i = 0; i < size; i++) {
        message[i] = sin(2 * M_PI * frequency * i / sampling_rate);
    }
    return 0;
}

// 数字调频（FSK）
int modulate_digital_frequency(double *cover, const int cover_len, const unsigned char *message, const int msg_len) {
    const double sampling_rate = 44100.0;
    const double f0 = 1000.0; // 比特0的频率
    const double f1 = 2000.0; // 比特1的频率
    const int samples_per_bit = (int)(sampling_rate / 1000); // 比特率1000 bps，每个比特44.1采样点（取整）

    if (cover_len < msg_len * samples_per_bit) {
        return -1; // 输出数组大小不足
    }

    for (int i = 0; i < msg_len; i++) {
        double frequency = message[i] ? f1 : f0;
        for (int j = 0; j < samples_per_bit; j++) {
            int index = i * samples_per_bit + j;
            cover[index] = sin(2 * M_PI * frequency * index / sampling_rate);
        }
    }
    return 0;
}

// 模拟调频（FM）
int modulate_analog_frequency(double *cover, const int cover_len, const double *message, const int msg_len) {
    const double sampling_rate = 44100.0;
    const double fc = 1000.0; // 载波频率
    const double delta_f = 500.0; // 频率偏移

    if (cover_len < msg_len) {
        return -1; // 输出数组大小不足
    }

    double phase = 0.0;
    for (int i = 0; i < msg_len; i++) {
        double instantaneous_frequency = fc + delta_f * message[i];
        phase += 2 * M_PI * instantaneous_frequency / sampling_rate;
        cover[i] = sin(phase);
    }
    return 0;
}

// 数字调幅（ASK）
int modulate_digital_amplitude(double *cover, const int cover_len, const unsigned char *message, const int msg_len) {
    const double sampling_rate = 44100.0;
    const double frequency = 1000.0; // 载波频率
    const int samples_per_bit = (int)(sampling_rate / 1000); // 比特率1000 bps

    if (cover_len < msg_len * samples_per_bit) {
        return -1; // 输出数组大小不足
    }

    for (int i = 0; i < msg_len; i++) {
        double amplitude = message[i] ? 1.0 : 0.0; // 比特1为全振幅，比特0为0
        for (int j = 0; j < samples_per_bit; j++) {
            int index = i * samples_per_bit + j;
            cover[index] = amplitude * sin(2 * M_PI * frequency * index / sampling_rate);
        }
    }
    return 0;
}

// 模拟调幅（AM）
int modulate_analog_amplitude(double *cover, const int cover_len, const double *message, const int msg_len) {
    const double sampling_rate = 44100.0;
    const double frequency = 1000.0; // 载波频率

    if (cover_len < msg_len) {
        return -1; // 输出数组大小不足
    }

    for (int i = 0; i < msg_len; i++) {
        double carrier = sin(2 * M_PI * frequency * i / sampling_rate);
        cover[i] = (1.0 + message[i]) * carrier; // 调制：振幅随消息信号变化
    }
    return 0;
}

// 数字调相（PSK）
int modulate_digital_phase(double *cover, const int cover_len, const unsigned char *message, const int msg_len) {
    const double sampling_rate = 44100.0;
    const double frequency = 1000.0; // 载波频率
    const int samples_per_bit = (int)(sampling_rate / 1000); // 比特率1000 bps

    if (cover_len < msg_len * samples_per_bit) {
        return -1; // 输出数组大小不足
    }

    for (int i = 0; i < msg_len; i++) {
        double phase_shift = message[i] ? M_PI : 0.0; // 比特1相位偏移π，比特0偏移0
        for (int j = 0; j < samples_per_bit; j++) {
            int index = i * samples_per_bit + j;
            cover[index] = sin(2 * M_PI * frequency * index / sampling_rate + phase_shift);
        }
    }
    return 0;
}

// 模拟调相（PM）
int modulate_analog_phase(double *cover, const int cover_len, const double *message, const int msg_len) {
    const double sampling_rate = 44100.0;
    const double frequency = 1000.0; // 载波频率
    const double k = 1.0; // 相位调制指数

    if (cover_len < msg_len) {
        return -1; // 输出数组大小不足
    }

    for (int i = 0; i < msg_len; i++) {
        cover[i] = sin(2 * M_PI * frequency * i / sampling_rate + k * message[i]);
    }
    return 0;
}